package com.kiwi.action;

import com.kiwi.constant.Constant;
import com.kiwi.constant.ConstantURL;
import com.kiwi.dao.IndianaDao;
import com.kiwi.util.EncryptSet.Constant.AES;
import com.kiwi.util.EncryptSet.Impl.SignatureServiceImpl;
import com.kiwi.util.RedisUtil;
import com.netease.shockwave.http.HttpHandler;
import com.netease.shockwave.http.HttpUtil;
import com.netease.shockwave.testng.ShockwaveTestNG;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginAction  extends ShockwaveTestNG {
    private static Logger logger = Logger.getLogger(LoginAction.class);
    @Autowired
    private RedisUtil redisUtil;
    //获取初始化id消息
//    public static Map<String, String> iniMobApp(String platform, String pdtVersion, String mac, String systemName,
//                                                String systemVersion, String resolution, String uniqueID, String deviceType, String deviceId)
    public  Map<String, String> iniMobApp(){
        String platform="xydai";
        String pdtVersion="1.0";
        String deviceId="02CEEB7A-BE45-485F-B493-4BFA4BC3BD40";
        String  mac="02CEEB7A-BE45-485F-B493-4BFA4BC3BD40";
        String systemName="ios";
        String systemVersion="10.3.2";
        String uniqueID="4E790E11-6030-4FE0-92AF-B955F61DC1EB";
        String  resolution="640*1136";
        String deviceType="my iPod";
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("platform", platform);
        params.put("pdtVersion", pdtVersion);
        params.put("deviceId", deviceId);
        params.put("mac", mac);
        params.put("systemName", systemName);
        params.put("systemVersion", systemVersion);
        params.put("uniqueID", uniqueID);
        params.put("resolution", resolution);
        params.put("deviceType", deviceType);
        HttpHandler handler = HttpHandler.getInstance();
        String enResult = handler.usingPostMethod(ConstantURL.INITMOBAPPURL, params, null);
        logger.info(enResult);
        JSONObject jsonRes = HttpUtil.parseJSONObject(enResult);
        HashMap<String, String> map = new HashMap<String, String>();
        if(jsonRes.getString("result").equals("100"))
        {
            String key=jsonRes.getJSONObject("data").getString("key");
            String id=jsonRes.getJSONObject("data").getString("id");
            logger.info("iniMobApp key: "+key+" id: "+id);
            map.put("key", key);
            map.put("id", id);
        }
        return map;
    }
    /*注册*/
    public String mobileReg(String mobile,String password,String verifyCode,String id,String deviceId) throws Exception {
        String param = String.format("mobile=%s&password=%s&verifyCode=%s", mobile,
                password, verifyCode);
        SignatureServiceImpl signatureService = new SignatureServiceImpl();
        String params = signatureService.encodeStr(param);
        HashMap<String, String> params_reg = new HashMap<String, String>();
        params_reg.put("id", id);
        params_reg.put("deviceId", deviceId);
        params_reg.put("params", params);
        HttpHandler handler = HttpHandler.getInstance();
        String enResult = handler.usingPostMethod(ConstantURL.MOBILEREGURL, params_reg, null);
        logger.info(enResult);
        return enResult;
    }
    /*删除注册信息*/
    public void delRegInfo(String mobile){
        String redisAccountInfo=redisUtil.get(Constant.USERINFO_BY_LOGINID_PREFIX+mobile);
        logger.info("mobile "+mobile+"已注册，账号id= "+redisAccountInfo);
        //账号已存在删除redis记录
        if(redisAccountInfo!=null)
            redisUtil.del(Constant.USERINFO_BY_LOGINID_PREFIX+mobile);
        IndianaDao indianaDao = (IndianaDao) applicationContext.getBean("IndianaDao");
        HashMap<String, String> userInfo= (HashMap<String, String>)indianaDao.selectOne("UserInfoMapper.SelectUserInfo", mobile);
        if(userInfo!=null) {
            String account_id = userInfo.get("account_id");
            String login_id = userInfo.get("login_id");
            logger.info("账号："+account_id + " 手机号 :" + login_id);
        }
        indianaDao.delete("UserInfoMapper.delectUserInfo", mobile);
    }
    //从redis中获取登录的sessionid
    public String getSessionidFromRedis(String mobile) throws Exception {

        IndianaDao indianaDao = (IndianaDao) applicationContext.getBean("IndianaDao");
        HashMap<String, String> userInfo= (HashMap<String, String>)indianaDao.selectOne("UserInfoMapper.SelectUserInfo", mobile);
        if(userInfo!=null) {
            String account_id = userInfo.get("account_id");
            String login_id = userInfo.get("login_id");
            String sessionId=redisUtil.get(Constant.API_UC_SESSION+account_id);
            logger.info("账号："+account_id + " 手机号 :" + login_id+" 已登录，sessionid= "+sessionId);
            return sessionId;
        }
        return null;
    }
    //从登录接口返回结果获取sessionid
    public String getSessionid(String id,String deviceId,String EncrytData) throws Exception {

        SignatureServiceImpl signatureService = new SignatureServiceImpl();
        String[] accountIdAndsessionId=signatureService.decodeStr(deviceId, id,  EncrytData).split("&");
        String sessionId=accountIdAndsessionId[1].substring(accountIdAndsessionId[1].lastIndexOf("=")+1);
        return sessionId;
    }
}
